IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = N'$(DatabaseName)') 
    ALTER DATABASE Workflow SET  
	ALLOW_SNAPSHOT_ISOLATION OFF
GO

IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = N'$(DatabaseName)') 
    ALTER DATABASE Workflow SET  
	READ_COMMITTED_SNAPSHOT OFF
GO

IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = N'$(DatabaseName)') 
    ALTER DATABASE $(DatabaseName) SET  
	MULTI_USER,
	CURSOR_CLOSE_ON_COMMIT OFF,
	CURSOR_DEFAULT LOCAL,
	AUTO_CLOSE OFF,
	AUTO_CREATE_STATISTICS ON,
	AUTO_SHRINK OFF,
	AUTO_UPDATE_STATISTICS ON,
	AUTO_UPDATE_STATISTICS_ASYNC ON,
	ANSI_NULL_DEFAULT ON,
	ANSI_NULLS ON,
	ANSI_PADDING ON,
	ANSI_WARNINGS ON,
	ARITHABORT ON,
	CONCAT_NULL_YIELDS_NULL ON,
	NUMERIC_ROUNDABORT OFF,
	QUOTED_IDENTIFIER ON,
	RECURSIVE_TRIGGERS OFF,
	RECOVERY FULL,
	PAGE_VERIFY NONE,
	DISABLE_BROKER,
	PARAMETERIZATION SIMPLE
	WITH ROLLBACK IMMEDIATE
GO

IF IS_SRVROLEMEMBER ('sysadmin') = 1
BEGIN

IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = N'$(DatabaseName)') 
    EXEC sp_executesql N'
    ALTER DATABASE $(DatabaseName) SET  
	DB_CHAINING OFF,
	TRUSTWORTHY OFF'

END
ELSE
BEGIN
    RAISERROR(N'Unable to modify the database settings for DB_CHAINING or TRUSTWORTHY. You must be a SysAdmin in order to apply these settings.',0,1)
END

GO

USE $(DatabaseName)

GO
-- Pre-Deployment Script Template							
----------------------------------------------------------------------------------------
-- This file contains SQL statements that will be executed before the build script	
-- Use SQLCMD syntax to include a file into the pre-deployment script			
-- Example:      :r .\filename.sql								
-- Use SQLCMD syntax to reference a variable in the pre-deployment script		
-- Example:      :setvar $TableName MyTable							
--               SELECT * FROM [$(TableName)]					
----------------------------------------------------------------------------------------

/*
IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE loginname = N'DISCOVERYLOGIC\huah')
CREATE LOGIN [DISCOVERYLOGIC\huah] FROM WINDOWS
*/

GO

PRINT N'Creating [dbo].[WorkflowTriggerInstance]'
GO
CREATE TABLE [dbo].[WorkflowTriggerInstance]
(
[WorkflowTriggerInstanceID] [varchar] (50) NULL,
[TriggerComponentID] [varchar] (50) NULL,
[WorkflowDesignID] [varchar] (50) NULL,
[TriggerType] [int] NULL,
[TimeSpanUnit] [int] NULL,
[Interval] [int] NULL,
[FirstStartTime] [datetime] NULL,
[LastRunTime] [datetime] NULL,
[NextRunTime] [datetime] NULL,
[IsActive] [bit] NULL
) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[SaveTriggerInstance]'
GO
CREATE PROCEDURE [dbo].[SaveTriggerInstance]
(@WorkflowTriggerInstanceID varchar(50),
@TriggerComponentID varchar(50),
@WorkflowDesignID varchar(50),
@TriggerType int,
@TimeSpanUnit int,
@Interval int, 
@FirstStartTime datetime,
@LastRunTime datetime,
@NextRunTime datetime,
@IsActive bit) as 
begin
	declare @count int
	set @count=(select count (*) from WorkflowTriggerInstance where WorkflowTriggerInstanceID=@WorkflowTriggerInstanceID)
	if(@count>0)
	begin
		update WorkflowTriggerInstance 
		set
			TriggerComponentID=@TriggerComponentID,
			WorkflowDesignID=@WorkflowDesignID,
			TriggerType=@TriggerType,
			TimeSpanUnit=@TimeSpanUnit,
			Interval=@Interval,
			FirstStartTime=@FirstStartTime,
			LastRunTime=@LastRunTime,
			NextRunTime=@NextRunTime,
			IsActive=@IsActive 
		where 
			WorkflowTriggerInstanceID=@WorkflowTriggerInstanceID
	end
	else
	begin
		insert into WorkflowTriggerInstance 
		(WorkflowTriggerInstanceID,
		TriggerComponentID,
		WorkflowDesignID,
		TriggerType,
		TimeSpanUnit,
		Interval,
		FirstStartTime,
		LastRunTime,
		NextRunTime,
		IsActive) values 
		(@WorkflowTriggerInstanceID,
		@TriggerComponentID,
		@WorkflowDesignID,
		@TriggerType,
		@TimeSpanUnit,
		@Interval,
		@FirstStartTime,
		@LastRunTime,
		@NextRunTime,
		@IsActive)
	end
end
GO
PRINT N'Creating [dbo].[GetWorkflowTriggerInstance]'
GO
CREATE PROCEDURE [dbo].[GetWorkflowTriggerInstance]
(@WorkflowTriggerInstanceID varchar(50)) as 
begin
	select * from WorkflowTriggerInstance where WorkflowTriggerInstanceID=@WorkflowTriggerInstanceID
end
GO
PRINT N'Creating [dbo].[JobOutput]'
GO
CREATE TABLE [dbo].[JobOutput]
(
[JobID] [varchar] (50) NOT NULL,
[Xdoc] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [PK__JobOutput__4222D4EF] on [dbo].[JobOutput]'
GO
ALTER TABLE [dbo].[JobOutput] ADD CONSTRAINT [PK__JobOutput__4222D4EF] PRIMARY KEY CLUSTERED  ([JobID]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[SaveJobOutput]'
GO
CREATE PROCEDURE [dbo].[SaveJobOutput]
(@JobID varchar(50), @Xdoc text) as 
begin
	declare @existCount int
	set @existCount=(select count (*) from JobOutput where JobID=@JobID)
	if(@existCount>0)
	begin
		update JobOutput set Xdoc=@Xdoc where JobID=@JobID
	end
	else
	begin
		insert into JobOutput (JobID, Xdoc) values (@JobID, @Xdoc)
	end
end
GO
PRINT N'Creating [dbo].[WorkflowDesign]'
GO
CREATE TABLE [dbo].[WorkflowDesign]
(
[WorkflowGuid] [varchar] (50) NOT NULL,
[WorkflowName] [varchar] (255) NULL,
[Version] [varchar] (25) NULL,
[CreationTime] [datetime] NULL,
[ModificationTime] [datetime] NULL,
[Author] [varchar] (255) NULL,
[Description] [varchar] (1000) NULL,
[FailureNotifyEmailAddress] [varchar] (255) NULL,
[XDoc] [text] NULL,
[DesignDocumentFilePath] [varchar] (1000) NULL,
[CheckOutTime] [datetime] NULL,
[CheckOutBy] [varchar] (255) NULL,
[IsLocked] [bit] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [PK__WorkflowDesign__7F60ED59] on [dbo].[WorkflowDesign]'
GO
ALTER TABLE [dbo].[WorkflowDesign] ADD CONSTRAINT [PK__WorkflowDesign__7F60ED59] PRIMARY KEY CLUSTERED  ([WorkflowGuid]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[SaveWorkflowDesign]'
GO
CREATE PROCEDURE [dbo].[SaveWorkflowDesign]
(
	@WorkflowGuid varchar(50),
	@WorkflowName varchar(255),
    @Version varchar(25),
    @CreationTime datetime,
    @ModificationTime datetime,
    @Author varchar(255),
    @Description varchar(1000),
    @FailureNotifyEmailAddress varchar(255),
	@XDoc text,
    @DesignDocumentFilePath varchar(1000),
    @CheckOutTime datetime,
    @CheckOutBy varchar(255),
    @IsLocked bit
) as 
begin
	declare @exist int
	set @exist=(select count(*) from WorkflowDesign where WorkflowGuid=@WorkflowGuid)
	
	if(@exist>0) begin
		update WorkflowDesign 
		set
			WorkflowName=@WorkflowName,
			Version=@Version,
			CreationTime=@CreationTime,
			ModificationTime=@ModificationTime,
			Author=@Author,
			Description=@Description,
			FailureNotifyEmailAddress=@FailureNotifyEmailAddress,
			XDoc=@XDoc,
			DesignDocumentFilePath=@DesignDocumentFilePath,
			CheckOutTime=@CheckOutTime,
			CheckOutBy=@CheckOutBy,
			IsLocked=@IsLocked 
		where 
			WorkflowGuid=@WorkflowGuid
	end
	else begin
		insert into WorkflowDesign 
			(WorkflowGuid, 
			WorkflowName,
			Version,
			CreationTime,
			ModificationTime,
			Author,
			Description,
			FailureNotifyEmailAddress,
			XDoc,
			DesignDocumentFilePath,
			CheckOutTime,
			CheckOutBy,
			IsLocked) 
		values 
			(@WorkflowGuid, 
			@WorkflowName,
			@Version,
			@CreationTime,
			@ModificationTime,
			@Author,
			@Description,
			@FailureNotifyEmailAddress,
			@XDoc,
			@DesignDocumentFilePath,
			@CheckOutTime,
			@CheckOutBy,
			@IsLocked)
	end 
END
GO
PRINT N'Creating [dbo].[GetAllWorkflowTriggerInstnacesByWorkflowDesign]'
GO
CREATE PROCEDURE [dbo].[GetAllWorkflowTriggerInstnacesByWorkflowDesign]
(@WorkflowDesignID varchar(50)) as 
begin
	select * from WorkflowTriggerInstance where WorkflowDesignID=@WorkflowDesignID order by LastRunTime
end
GO
PRINT N'Creating [dbo].[GetAllWorkflowDesigns]'
GO
CREATE PROCEDURE [dbo].[GetAllWorkflowDesigns]
AS
begin
	select * from WorkflowDesign order by WorkflowName
end
GO
PRINT N'Creating [dbo].[GetLastWorkflowTriggerInstanceByTriggerComponentID]'
GO
CREATE PROCEDURE [dbo].[GetLastWorkflowTriggerInstanceByTriggerComponentID]
(@TriggerComponentID varchar(50)) as 
begin
	select * from WorkflowTriggerInstance where TriggerComponentID=@TriggerComponentID order by LastRunTime desc
end
GO
PRINT N'Creating [dbo].[WoxFiles]'
GO
CREATE TABLE [dbo].[WoxFiles]
(
[WorkflowGuid] [varchar] (50) NOT NULL,
[Version] [varchar] (10) NOT NULL,
[Author] [varchar] (100) NULL,
[UpdatedOn] [datetime] NULL,
[WoxFile] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [pk_WoxFiles] on [dbo].[WoxFiles]'
GO
ALTER TABLE [dbo].[WoxFiles] ADD CONSTRAINT [pk_WoxFiles] PRIMARY KEY CLUSTERED  ([WorkflowGuid], [Version]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[GetWoxFile]'
GO
CREATE PROCEDURE [dbo].[GetWoxFile]
	@WorkflowGuid VARCHAR(50) 
AS
	SELECT WoxFile FROM WoxFiles WHERE WorkflowGuid=@WorkflowGuid AND Version IN (
		SELECT MAX(Version) FROM WoxFiles WHERE WorkflowGuid=@WorkflowGuid);
RETURN 0;
GO
PRINT N'Creating [dbo].[GetWorkflowDesignByFilePath]'
GO
CREATE PROCEDURE [dbo].[GetWorkflowDesignByFilePath]
(@WorkflowStationName varchar(100),
@FilePath varchar(500)) as 
begin
	select * from WorkflowDesign where 
	DesignDocumentFilePath like '%'+@WorkflowStationName+';'+@FilePath+'%'
end
GO
PRINT N'Creating [dbo].[AddTriggerInstnace]'
GO
CREATE PROCEDURE [dbo].[AddTriggerInstnace]
(@WorkflowTriggerInstanceID varchar(50),
@TriggerComponentID varchar(50),
@WorkflowDesignID varchar(50),
@TriggerType int,
@TimeSpanUnit int,
@Interval int, 
@FirstStartTime datetime,
@LastRunTime datetime,
@NextRunTime datetime,
@IsActive bit) as 
begin
	insert into WorkflowTriggerInstance 
	(WorkflowTriggerInstanceID,
	TriggerComponentID,
	WorkflowDesignID,
	TriggerType,
	TimeSpanUnit,
	Interval,
	FirstStartTime,
	LastRunTime,
	NextRunTime,
	IsActive) values 
	(@WorkflowTriggerInstanceID,
	@TriggerComponentID,
	@WorkflowDesignID,
	@TriggerType,
	@TimeSpanUnit,
	@Interval,
	@FirstStartTime,
	@LastRunTime,
	@NextRunTime,
	@IsActive)
end
GO
PRINT N'Creating [dbo].[GetWorkflowDesign]'
GO
CREATE PROCEDURE [dbo].[GetWorkflowDesign]
(@WorkflowGuid varchar(50)) as 
begin
	select * from WorkflowDesign where WorkflowGuid=@WorkflowGuid
end
GO
PRINT N'Creating [dbo].[PngFiles]'
GO
CREATE TABLE [dbo].[PngFiles]
(
[WorkflowGuid] [varchar] (50) NOT NULL,
[Version] [varchar] (10) NOT NULL,
[Author] [varchar] (100) NULL,
[UpdatedOn] [datetime] NULL,
[PngFile] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [pk_PngFiles] on [dbo].[PngFiles]'
GO
ALTER TABLE [dbo].[PngFiles] ADD CONSTRAINT [pk_PngFiles] PRIMARY KEY CLUSTERED  ([WorkflowGuid], [Version]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[GetPngFile]'
GO
CREATE PROCEDURE [dbo].[GetPngFile]
	@WorkflowGuid VARCHAR(50) 
AS
	SELECT PngFile FROM PngFiles WHERE WorkflowGuid=@WorkflowGuid AND Version IN (
		SELECT MAX(Version) FROM PngFiles WHERE WorkflowGuid=@WorkflowGuid);
RETURN 0;
GO
PRINT N'Creating [dbo].[GetLastWorkflowTriggerInstanceByWorkflowDesign]'
GO
CREATE PROCEDURE [dbo].[GetLastWorkflowTriggerInstanceByWorkflowDesign]
(@WorkflowDesignID varchar(50)) as 
begin
	select * from WorkflowTriggerInstance where WorkflowDesignID=@WorkflowDesignID order by LastRunTime desc
end
GO
PRINT N'Creating [dbo].[Jobs]'
GO
CREATE TABLE [dbo].[Jobs]
(
[JobID] [varchar] (50) NOT NULL,
[RunID] [varchar] (50) NULL,
[ComponentID] [varchar] (50) NULL,
[ComponentTypeName] [varchar] (500) NULL,
[Cancelled] [bit] NULL,
[Dependency] [varchar] (max) NULL,
[HasError] [bit] NULL,
[IsFirstJob] [bit] NULL,
[IsLastJob] [bit] NULL,
[JobInstructionTypeName] [varchar] (500) NULL,
[JobOutputTypeName] [varchar] (500) NULL,
[JobInstructionID] [int] NULL,
[JobOutputID] [int] NULL,
[Priority] [int] NULL,
[ReceiveTime] [datetime] NULL,
[SubmissionTime] [datetime] NULL,
[StartTime] [datetime] NULL,
[FinishTime] [datetime] NULL
) ON [PRIMARY]
GO
PRINT N'Creating primary key [PK__Jobs__3B75D760] on [dbo].[Jobs]'
GO
ALTER TABLE [dbo].[Jobs] ADD CONSTRAINT [PK__Jobs__3B75D760] PRIMARY KEY CLUSTERED  ([JobID]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[SaveJob]'
GO
CREATE PROCEDURE [dbo].[SaveJob]
(
	@JobID varchar(50), 
	@RunID varchar(50),
	@ComponentID varchar(50),
	@ComponentTypeName varchar(500),
	@Cancelled bit,
	@Dependency varchar(500),
	@HasError bit,
	@IsFirstJob bit,
	@IsLastJob bit,
	@JobInstructionTypeName varchar(500),
	@JobOutputTypeName varchar(500),
	@JobInstructionID int,
	@JobOutputID int,
	@Priority int,
	@ReceiveTime datetime,
	@SubmissionTime datetime,
	@StartTime datetime,
	@FinishTime datetime
) as 
begin 
	declare @existCount int
	set @existCount=(select count (*) from Jobs where JobID=@JobID)
	
	if(@existCount>0)
	begin
		update Jobs 
		set 
			RunID=@RunID,
			ComponentID=@ComponentID,
			ComponentTypeName=@ComponentTypeName,
			Cancelled=@Cancelled,
			Dependency=@Dependency,
			HasError=@HasError,
			IsFirstJob=@IsFirstJob,
			IsLastJob=@IsLastJob,
			JobInstructionTypeName=@JobInstructionTypeName,
			JobOutputTypeName=@JobOutputTypeName,
			JobInstructionID=@JobInstructionID,
			JobOutputID=@JobOutputID,
			Priority=@Priority,
			ReceiveTime=@ReceiveTime,
			SubmissionTime=@SubmissionTime,
			StartTime=@StartTime,
			FinishTime=@FinishTime 
		where 
			JobID=@JobID
	end
	else
	begin
		insert into Jobs 
			(JobID,
			RunID,
			ComponentID,
			ComponentTypeName,
			Cancelled,
			Dependency,
			HasError,
			IsFirstJob,
			IsLastJob,
			JobInstructionTypeName,
			JobOutputTypeName,
			JobInstructionID,
			JobOutputID,
			Priority,
			ReceiveTime,
			SubmissionTime,
			StartTime,
			FinishTime) 
		values 
			(@JobID, 
			@RunID,
			@ComponentID,
			@ComponentTypeName,
			@Cancelled,
			@Dependency,
			@HasError,
			@IsFirstJob,
			@IsLastJob,
			@JobInstructionTypeName,
			@JobOutputTypeName,
			@JobInstructionID,
			@JobOutputID,
			@Priority,
			@ReceiveTime,
			@SubmissionTime,
			@StartTime,
			@FinishTime)
	end
end
GO
PRINT N'Creating [dbo].[SavePngFile]'
GO
CREATE PROCEDURE [dbo].[SavePngFile]
	@WorkflowGuid VARCHAR(50),
	@Version VARCHAR(10),
	@Author VARCHAR(100),
	@UpdatedOn DATETIME,
	@PngFile IMAGE 
AS
	IF NOT EXISTS (SELECT * FROM PngFiles WHERE WorkflowGuid=@WorkflowGuid AND Version=@Version)
	BEGIN
		INSERT INTO PngFiles (
			WorkflowGuid,
			Version,
			Author,
			UpdatedOn,
			PngFile) VALUES (
			@WorkflowGuid,
			@Version,
			@Author,
			@UpdatedOn,
			@PngFile)
	END
RETURN 0;
GO
PRINT N'Creating [dbo].[GetJobByRunAndComponent]'
GO
CREATE PROCEDURE [dbo].[GetJobByRunAndComponent]
(@RunID varchar(50), @ComponentID varchar(50)) as 
begin
	select * from Jobs 
	where 
		RunID=@RunID and ComponentID=@ComponentID
end
GO
PRINT N'Creating [dbo].[GetJobOutput]'
GO
CREATE PROCEDURE [dbo].[GetJobOutput]
(@JobID varchar(50)) as 
begin
	select * from JobOutput where JobID=@JobID
end
GO
PRINT N'Creating [dbo].[Runs]'
GO
CREATE TABLE [dbo].[Runs]
(
[RunID] [varchar] (50) NOT NULL,
[WorkflowID] [varchar] (50) NULL,
[TriggerInstanceID] [varchar] (50) NULL,
[SubmitTime] [datetime] NULL,
[StartTime] [datetime] NULL,
[FinishTime] [datetime] NULL,
[Vars] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [PK__Runs__2E1BDC42] on [dbo].[Runs]'
GO
ALTER TABLE [dbo].[Runs] ADD CONSTRAINT [PK__Runs__2E1BDC42] PRIMARY KEY CLUSTERED  ([RunID]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[SaveRun]'
GO
CREATE PROCEDURE [dbo].[SaveRun]
(@RunID varchar(50),
@WorkflowID varchar(50), 
@TriggerInstanceID varchar(50), 
@SubmitTime datetime,
@StartTime datetime, 
@FinishTime datetime, 
@Vars text) as 
begin
	declare @count int 
	set @count =(select count (*) from Runs where RunID=@RunID)
	if @count > 0 
	begin
		update Runs 
		set 
			WorkflowID=@WorkflowID,
			TriggerInstanceID=@TriggerInstanceID,
			SubmitTime=@SubmitTime,
			StartTime=@StartTime,
			FinishTime=@FinishTime, 
			Vars=@Vars 
		where 
			RunID=@RunID
	end
	else
	begin
		insert into Runs 
			(RunID,
			WorkflowID,
			TriggerInstanceID,
			SubmitTime,
			StartTime,
			FinishTime, 
			Vars) values 
			(@RunID,
			@WorkflowID,
			@TriggerInstanceID,
			@SubmitTime,
			@StartTime,
			@FinishTime,
			@Vars)
	end
end
GO
PRINT N'Creating [dbo].[DeleteWorkflowDesign]'
GO
CREATE PROCEDURE [dbo].[DeleteWorkflowDesign]
(@WorkflowGuid varchar(50)) as 
begin
	delete WorkflowDesign where WorkflowGuid=@WorkflowGuid
end
GO
PRINT N'Creating [dbo].[GetRunByWorkflow]'
GO
CREATE PROCEDURE [dbo].[GetRunByWorkflow]
@WorkflowID varchar(50) 
AS
begin
	select * from Runs where WorkflowID=@WorkflowID
end
GO
PRINT N'Creating [dbo].[GetJob]'
GO
CREATE PROCEDURE [dbo].[GetJob]
(@JobID varchar(50)) as 
begin
	select * from Jobs where JobID=@JobID
end
GO
PRINT N'Creating [dbo].[GetRun]'
GO
CREATE PROCEDURE [dbo].[GetRun]
(@RunID varchar(50)) as 
begin
	select * from Runs where RunID=@RunID
end
GO
PRINT N'Creating [dbo].[GetAllJobsForRun]'
GO
CREATE PROCEDURE [dbo].[GetAllJobsForRun]
@RunID varchar(50) 
AS
begin
	select * from Jobs where RunID=@RunID
end
GO
PRINT N'Creating [dbo].[JobInstruction]'
GO
CREATE TABLE [dbo].[JobInstruction]
(
[JobID] [varchar] (50) NOT NULL,
[Xdoc] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT N'Creating primary key [PK__JobInstruction__46E78A0C] on [dbo].[JobInstruction]'
GO
ALTER TABLE [dbo].[JobInstruction] ADD CONSTRAINT [PK__JobInstruction__46E78A0C] PRIMARY KEY CLUSTERED  ([JobID]) ON [PRIMARY]
GO
PRINT N'Creating [dbo].[GetJobInstruction]'
GO
CREATE PROCEDURE [dbo].[GetJobInstruction]
(@JobID varchar(50)) as 
begin
	select * from JobInstruction where JobID=@JobID
end
GO
PRINT N'Creating [dbo].[SaveWoxFile]'
GO
CREATE PROCEDURE [dbo].[SaveWoxFile]
	@WorkflowGuid VARCHAR(50),
	@Version VARCHAR(10),
	@Author VARCHAR(100),
	@UpdatedOn DATETIME,
	@WoxFile IMAGE 
AS
	IF NOT EXISTS (SELECT * FROM WoxFiles WHERE WorkflowGuid=@WorkflowGuid AND Version=@Version)
	BEGIN
		INSERT INTO WoxFiles (
			WorkflowGuid,
			Version,
			Author,
			UpdatedOn,
			WoxFile) VALUES (
			@WorkflowGuid,
			@Version,
			@Author,
			@UpdatedOn,
			@WoxFile)
	END
RETURN 0;
GO
PRINT N'Creating [dbo].[SaveJobInstruction]'
GO
CREATE PROCEDURE [dbo].[SaveJobInstruction]
(@JobID varchar(50), @Xdoc text) as 
begin
	declare @existCount int
	set @existCount=(select count (*) from JobInstruction where JobID=@JobID)
	if(@existCount>0)
	begin
		update JobInstruction set Xdoc=@Xdoc where JobID=@JobID
	end
	else
	begin
		insert into JobInstruction (JobID, Xdoc) values (@JobID, @Xdoc)
	end
end
GO
PRINT N'Creating [dbo].[WorkflowComponents]'
GO
CREATE TABLE [dbo].[WorkflowComponents]
(
[ComponentID] [varchar] (50) NOT NULL,
[WorkflowID] [varchar] (50) NULL,
[ComponentName] [varchar] (255) NULL
) ON [PRIMARY]
GO